package com.xiaobai.dish.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaobai.common.base.CommonRuntimeException;
import com.xiaobai.common.base.ErrorCode;
import com.xiaobai.dish.dao.IDishDao;
import com.xiaobai.dish.request.*;
import com.xiaobai.dish.response.DishRes;
import com.xiaobai.upload.UploadController;
import com.xiaobai.util.FileUtil;
import com.xiaobai.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 菜品查询Service
 *
 * @author bail
 * @date 2018/12/31.13:09
 */
@Service
@Slf4j
public class DishService {
    @Autowired
    private IDishDao dishDao;

    /**
     * 查询菜品分页数据
     */
    public PageInfo<DishRes> queryDishPage(DishQueryListReq dishQueryListReq) {
        dishQueryListReq.setCreateUserId(LoginUtil.getUserId());
        PageHelper.startPage(dishQueryListReq.getPageNum(), dishQueryListReq.getPageSize());
        return new PageInfo<>(dishDao.queryDishList(dishQueryListReq));
    }

    /**
     * 查询单个菜品数据
     */
    public DishRes queryOneDish(DishQueryOneReq dishQueryOneReq) {
        dishQueryOneReq.setCreateUserId(LoginUtil.getUserId());
        DishRes dishRes = dishDao.queryOneDish(dishQueryOneReq);
        if (dishRes == null) {
            throw new CommonRuntimeException(ErrorCode.COMMON_FAIL, "您要查看的菜品不存在");
        }
        return dishRes;
    }

    /**
     * 添加菜品
     */
    public void addDish(DishAddReq dishAddReq) {
        dishAddReq.setCreateUserId(LoginUtil.getUserId());
        dishDao.addDish(dishAddReq);
    }

    /**
     * 增加热度(just eat it数量加1)
     */
    public void incrHotGrade(DishIncrHotGradeReq dishIncrHotGradeReq) {
        dishDao.incrHotGrade(dishIncrHotGradeReq);
    }

    /**
     * 编辑菜品
     */
    public void updateDish(DishUpdateReq dishUpdateReq) {
        dishUpdateReq.setCreateUserId(LoginUtil.getUserId());
        dishDao.updateDish(dishUpdateReq);
    }

    /**
     * 删除菜品图片
     */
    public void deleteDishPic(DishPicDelReq dishPicDelReq) {
        // 1.查询待删除的图片是否已经入库
        DishQueryOneReq dishQueryOneReq = new DishQueryOneReq();
        dishQueryOneReq.setDishPic(dishPicDelReq.getDishPic());
        DishRes dishRes = dishDao.queryOneDish(dishQueryOneReq);

        if (dishRes == null) {
            // 2.未入库,则可以直接删除
            // ☆☆☆一定要注意:只能删单个文件,若用户传入的是一个文件夹是十分危险的操作☆☆☆
            FileUtil.delFile(UploadController.DIR_PATH_PREFIX + dishPicDelReq.getDishPic());
        } else {
            // 3.已入库,则校验该图片是否隶属于当前登录人
            Long userId = LoginUtil.getUserId();
            if (dishRes.getCreateUserId().equals(userId)) {
                // 4.1.隶属于登录人,则可以删除图片
                FileUtil.delFile(UploadController.DIR_PATH_PREFIX + dishPicDelReq.getDishPic());

                // 4.2.清空菜品图片字段
                dishPicDelReq.setDishId(dishRes.getDishId());
                dishPicDelReq.setCreateUserId(dishRes.getCreateUserId());
                dishDao.deleteDishPic(dishPicDelReq);
            } else {
                throw new CommonRuntimeException(ErrorCode.COMMON_FAIL, "您没有权限删除该图片");
            }
        }
    }

    /**
     * 删除菜品
     */
    public void deleteDish(DishDelReq dishDelReq) {
        dishDelReq.setCreateUserId(LoginUtil.getUserId());

        // 1.校验待删除的菜品是否属于登录人
        DishQueryOneReq dishQueryOneReq = new DishQueryOneReq();
        BeanUtils.copyProperties(dishDelReq, dishQueryOneReq);
        DishRes dishRes = dishDao.queryOneDish(dishQueryOneReq);
        if (dishRes == null) {
            throw new CommonRuntimeException(ErrorCode.COMMON_FAIL, "您没有权限删除该菜品");
        }

        // 2.删除物理磁盘上的图片
        FileUtil.delFile(UploadController.DIR_PATH_PREFIX + dishRes.getDishPic());

        // 3.删除该菜品数据
        dishDao.deleteDish(dishDelReq);
    }
}
